﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// byte数组扩展
    /// </summary>
    public static class ArrayExtension
    {
        /// <summary>
        /// 使用系统默认字符集转字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GToString(this byte[] data)
        {
            return Encoding.Default.GetString(data);
        }

        /// <summary>
        /// 使用指定字符集转字符串
        /// </summary>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GToString(this byte[] data,Encoding encoding)
        {
            return encoding.GetString(data);
        }

        /// <summary>
        /// 将byte数据转为十六进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GToHexString(this byte[] data)
        {
            var r = string.Empty;
            foreach(var b in data)
            {
                r += b.ToString("X2");
            }
            return r;
        }
    }
}
